import request from './request'

/**
 * 课程考核组成部分相关接口
 */
export const courseExamPartApi = {
  /**
   * 获取教学任务的所有考核组成部分
   * @param {number} teachingMissionId 教学任务ID
   */
  getExamPartsByTeachingMission(teachingMissionId) {
    return request({
      url: `/api/course-exam-parts/teaching-mission/${teachingMissionId}`,
      method: 'get'
    }).then(data => {
      // 处理返回的数据，将proportion转换为percentage
      if (Array.isArray(data)) {
        return data.map(item => ({
          ...item,
          percentage: item.proportion, // 添加percentage字段
          // 兼容旧字段，根据examPartType设置
          isFormative: item.examPartType === 'FORMATIVE',
          isFinal: item.examPartType === 'FINAL'
        }));
      }
      return data;
    });
  },

  /**
   * 根据ID获取考核组成部分
   * @param {number} id 考核组成部分ID
   */
  getExamPartById(id) {
    return request({
      url: `/api/course-exam-parts/${id}`,
      method: 'get'
    })
  },

  /**
   * 创建新的考核组成部分
   * @param {Object} examPart 考核组成部分信息
   */
  createExamPart(examPart) {
    return request({
      url: '/api/course-exam-parts',
      method: 'post',
      data: examPart
    })
  },

  /**
   * 更新考核组成部分
   * @param {number} id 考核组成部分ID
   * @param {Object} examPart 考核组成部分信息
   */
  updateExamPart(id, examPart) {
    return request({
      url: `/api/course-exam-parts/${id}`,
      method: 'put',
      data: examPart
    })
  },

  /**
   * 创建考核组成部分（简化调用方法）
   */
  create(examPart) {
    return this.createExamPart(examPart)
  },

  /**
   * 更新考核组成部分（简化调用方法）
   */
  update(id, examPart) {
    return this.updateExamPart(id, examPart)
  },

  /**
   * 删除考核组成部分
   */
  delete(id) {
    return request({
      url: `/api/course-exam-parts/${id}`,
      method: 'delete'
    })
  },

  /**
   * 自动生成考核组成
   */
  autoGenerate(teachingMissionId) {
    return request({
      url: `/api/course-exam-parts/auto-generate/${teachingMissionId}`,
      method: 'post'
    })
  },

  /**
   * 复制考核组成到其他教学任务
   * @param {number} sourceMissionId 源教学任务ID
   * @param {Array<number>} targetMissionIds 目标教学任务ID数组
   */
  copyExamPartsToOtherMissions(sourceMissionId, targetMissionIds) {
    return request({
      url: `/api/course-exam-parts/copy/${sourceMissionId}`,
      method: 'post',
      data: targetMissionIds
    })
  },

  /**
   * 获取同学期同课程的其他未设置考核组成的教学任务
   * @param {number} teachingMissionId 当前教学任务ID
   */
  getTargetMissionsForCopy(teachingMissionId) {
    return request({
      url: `/api/course-exam-parts/copy-targets/${teachingMissionId}`,
      method: 'get'
    })
  }
}

// 扩展 courseExamPartApi 对象
export const courseExamPartApiExt = {
  /**
   * 删除考核组成部分
   * @param {number} id 考核组成部分ID
   */
  deleteExamPart(id) {
    return request({
      url: `/api/course-exam-parts/${id}`,
      method: 'delete'
    })
  },

  /**
   * 批量保存教学任务的考核组成部分
   * @param {number} teachingMissionId 教学任务ID
   * @param {Array} examParts 考核组成部分列表
   */
  saveExamParts(teachingMissionId, examParts) {
    // 处理数据，确保属性名称与后台模型一致
    const formattedData = examParts.map(part => {
      // 根据旧的Boolean字段确定examPartType
      let examPartType = 'OTHER'; // 默认值
      if (part.isFormative) {
        examPartType = 'FORMATIVE';
      } else if (part.isFinal) {
        examPartType = 'FINAL';
      } else if (part.examPartType) {
        examPartType = part.examPartType;
      }

      return {
        id: part.id,
        name: part.name,
        teachingMissionId: teachingMissionId,
        proportion: part.proportion, // 使用proportion字段
        scoreTypeId: part.scoreTypeId,
        examPartType: examPartType
      };
    });

    return request({
      url: `/api/course-exam-parts/batch/${teachingMissionId}`,
      method: 'post',
      data: formattedData
    })
  },

  /**
   * 复制考核组成到其他教学任务
   * @param {number} sourceMissionId 源教学任务ID
   * @param {Array<number>} targetMissionIds 目标教学任务ID数组
   */
  copyExamPartsToOtherMissions(sourceMissionId, targetMissionIds) {
    return request({
      url: `/api/course-exam-parts/copy/${sourceMissionId}`,
      method: 'post',
      data: targetMissionIds
    })
  },

  /**
   * 获取同学期同课程的其他未设置考核组成的教学任务
   * @param {number} teachingMissionId 当前教学任务ID
   */
  getTargetMissionsForCopy(teachingMissionId) {
    return request({
      url: `/api/course-exam-parts/copy-targets/${teachingMissionId}`,
      method: 'get'
    })
  },

  /**
   * 批量设置多个教学任务的考核组成
   * @param {Array} examParts 考核组成模板列表
   * @param {Array<number>} teachingMissionIds 目标教学任务ID数组
   */
  batchSetExamParts(examParts, teachingMissionIds) {
    return request({
      url: '/api/course-exam-parts/batch-set',
      method: 'post',
      data: {
        examParts: examParts,
        teachingMissionIds: teachingMissionIds
      }
    })
  },

  /**
   * 获取跨学期复制的目标教学任务列表
   * @param {number} sourceMissionId 源教学任务ID
   * @param {number} termId 目标学期ID，可选
   */
  getCrossTermTargetMissionsForCopy(sourceMissionId, termId) {
    const params = termId ? { termId } : {};
    return request({
      url: `/api/course-exam-parts/cross-term-copy-targets/${sourceMissionId}`,
      method: 'get',
      params: params
    })
  }
}
